package worldutils.huntgame;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class CompassGeter {



    //这个类存储了指南针追踪的玩家，通过HashMap
//    目前没有思路，先不写。




    public static HashMap<UUID, String> playerTags = new HashMap<>();


    public static void addTag(Player player, String tag) {
        removeTag(player);
        playerTags.put(player.getUniqueId(), tag);
        player.sendMessage(ChatColor.GREEN + HuntGame.getMessage("you-become-tag").replace("*t", tag));
        player.setPlayerListName(getTag(player) + " " + player.getName());
    }

    public static String getTag(Player player) {
        return playerTags.getOrDefault(player.getUniqueId(), HuntGame.getMessage("no-tag"));
    }

    public static void removeTag(Player player) {
        if (playerTags.containsKey(player.getUniqueId())) {
            playerTags.remove(player.getUniqueId());
        } else {
            player.sendMessage(ChatColor.GRAY + HuntGame.getMessage("not-have-tag"));
        }
    }

    public static int getTagCount(String tag) {
        int count = 0;

        for (String playerTag : playerTags.values()) {
            if (playerTag.equalsIgnoreCase(tag)) {
                count++;
            }
        }

        return count;
    }

    public static final String[] TAGS = {HuntGame.getMessage("hunter"), HuntGame.getMessage("runner")};
    public static final Random RANDOM = new Random();

    public static String getRandomTag() {
        int index = RANDOM.nextInt(TAGS.length);
        return TAGS[index];
    }

    public static String getCloseRangePlayer(String tag, Player runner) {
        World world = runner.getWorld();
        Player closestPlayer = null;
        double closestDistance = Double.MAX_VALUE;

        for (Player player : world.getPlayers()) {
            // 检查玩家是否有标签以及标签是否匹配
            if (getTag(player).equalsIgnoreCase(tag)) {
                double distance = runner.getLocation().distance(player.getLocation());
                if (distance < closestDistance) {
                    closestDistance = distance;
                    closestPlayer = player;
                }
            }
        }

        if (closestPlayer == null) {
            ItemStack itemInHand = runner.getInventory().getItemInMainHand();
            if (itemInHand != null && itemInHand.getType() == Material.COMPASS) {
                return HuntGame.getMessage("compass-action-non-player").replace("*t", tag);
            }
        }
        ItemStack itemInHand = runner.getInventory().getItemInMainHand();
        if (itemInHand != null && itemInHand.getType() == Material.COMPASS) {
            return String.format(HuntGame.getMessage("compass-action").replace("*p", "%s").replace("*r", "%.2f"), closestPlayer.getName(), closestDistance);
        }
        return "";
    }

    public static String getCloseRangehunterPlayer(String tag, Player runner) {
        World world = runner.getWorld();
        Player closestPlayer = null;
        double closestDistance = Double.MAX_VALUE;

        for (Player player : world.getPlayers()) {
            // 检查玩家是否有标签以及标签是否匹配
            if (getTag(player).equalsIgnoreCase(tag)) {
                double distance = runner.getLocation().distance(player.getLocation());
                if (distance < closestDistance) {
                    closestDistance = distance;
                    closestPlayer = player;
                }
            }
        }

        if (closestPlayer == null) {
            ItemStack itemInHand = runner.getInventory().getItemInMainHand();
            if (itemInHand != null && itemInHand.getType() == Material.COMPASS) {
                return "";
            }
        }
        ItemStack itemInHand = runner.getInventory().getItemInMainHand();
        if (closestDistance <= HuntGame.warnrange) {
            return String.format(HuntGame.getMessage("close-range-hunter").replace("*p", "%s").replace("*r", "%.2f"), closestPlayer.getName(), closestDistance);
        }
        return "";
    }
}
